import { NextRequest, NextResponse } from 'next/server';
import { getMonitoringManager, isServicesInitialized } from '@/lib/services-init';
import { createModuleLogger } from '@/utils/logger';

const logger = createModuleLogger('admin-monitoring-status');

export async function GET(request: NextRequest) {
  try {
    const servicesReady = isServicesInitialized();
    const monitoringManager = getMonitoringManager();
    
    if (!monitoringManager) {
      return NextResponse.json(
        { 
          success: false, 
          error: '监控管理器未初始化',
          servicesReady,
          timestamp: new Date().toISOString()
        },
        { status: 503 }
      );
    }

    const status = monitoringManager.getAllStatus();
    
    return NextResponse.json({
      success: true,
      servicesReady,
      monitoring: {
        isRunning: status.activeCount > 0,
        activeChains: status.activeCount,
        totalChains: status.monitorCount,
        chains: status.chains
      },
      environment: {
        nodeEnv: process.env.NODE_ENV,
        autoStartEnabled: process.env.AUTO_START_MONITORING === 'true'
      },
      timestamp: new Date().toISOString()
    });
    
  } catch (error) {
    logger.error('❌ 获取监控状态失败:', error);
    return NextResponse.json(
      { 
        success: false, 
        error: '获取监控状态失败',
        details: error instanceof Error ? error.message : String(error),
        timestamp: new Date().toISOString()
      },
      { status: 500 }
    );
  }
} 